using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DevExpress.Web.ASPxTabControl;
using DevExpress.Web.ASPxNewsControl;
using Indeco.G24.Data;
using Indeco.G24.Data.SqlClient;
using Indeco.G24.Entities; 

namespace Indeco.G24.Website.News
{
    public partial class Category : G24.Web.G24Page
    {
        public string IdDanhmuc, IdTinTuc;
        protected void Page_Load(object sender, EventArgs e)
        {
            IdDanhmuc = Request.Params["IDTinTucDanhMuc"];           
            string sqlNews, sqlTieuDiem, sqlSuKien;
            IdTinTuc = Request.Params["IDTinTuc"];
            string sqlNewsOther, sqlFindDanhmuc;
            if (string.IsNullOrEmpty(IdTinTuc) && string.IsNullOrEmpty(IdDanhmuc))
            {
                ListNewsOfCategory.Visible = false;
                NewsDetailOfCategory.Visible = false;
            }
            if (!string.IsNullOrEmpty(IdDanhmuc))
            {
                ListNewsOfCategory.Visible = true;
                NewsDetailOfCategory.Visible = false;
                int outcount;
                TList<TinTucDanhMuc> danhmuc = DataRepository.TinTucDanhMucProvider.Find("ID_TinTucDanhMuc='"+IdDanhmuc+"'");                
                if (danhmuc[0].IDTinTucDanhMucCha.Equals(Guid.Empty))
                {
                    sqlNews = "id_tintucdanhmuc in (select id_tintucdanhmuc from tb_TinTucDanhMuc where id_tintucdanhmuccha='" + IdDanhmuc + "') and nhacungcap is null";
                    sqlTieuDiem = "id_tintuc in (select id_tintuc from tb_tintuctieudiem) and id_tintucdanhmuc in (select id_tintucdanhmuc from tb_TinTucDanhMuc where id_tintucdanhmuccha='" + IdDanhmuc + "') and id_tintuc not in (select top 5 id_tintuc from tb_tintuc where id_tintucdanhmuc in(select id_tintucdanhmuc from tb_TinTucDanhMuc where id_tintucdanhmuccha='" + IdDanhmuc + "')order by thoidiemcapnhat desc) and nhacungcap is null";
                    sqlSuKien = "id_tintuc in (select id_tintuc from tb_tintucsukien) and id_tintucdanhmuc in (select id_tintucdanhmuc from tb_TinTucDanhMuc where id_tintucdanhmuccha='" + IdDanhmuc + "') and id_tintuc not in (select top 5 id_tintuc from tb_tintuc where id_tintucdanhmuc in(select id_tintucdanhmuc from tb_TinTucDanhMuc where id_tintucdanhmuccha='" + IdDanhmuc + "')order by thoidiemcapnhat desc) and nhacungcap is null";
                }
                else
                {
                    sqlNews = "id_tintucdanhmuc ='" + IdDanhmuc + "' and nhacungcap is null";
                    sqlTieuDiem = "id_tintuc in (select id_tintuc from tb_tintuctieudiem) and id_tintucdanhmuc= '" + IdDanhmuc + "' and id_tintuc not in (select top 5 id_tintuc from tb_tintuc where id_tintucdanhmuc= '" + IdDanhmuc + "' order by thoidiemcapnhat desc) and nhacungcap is null";
                    sqlSuKien = "id_tintuc in (select id_tintuc from tb_tintucsukien) and id_tintucdanhmuc ='" + IdDanhmuc + "' and id_tintuc not in (select top 5 id_tintuc from tb_tintuc where id_tintucdanhmuc ='" + IdDanhmuc + "' order by thoidiemcapnhat desc) and nhacungcap is null";
                }
                TList<TinTuc> tintuc = DataRepository.TinTucProvider.GetPaged(sqlNews, "thoidiemcapnhat DESC", 0, 5, out outcount);
                ListTinTuc.DataSource = tintuc;
                ListTinTuc.DataBind();               
                TList<TinTuc> tintieudiem = DataRepository.TinTucProvider.GetPaged(sqlTieuDiem, "thoidiemcapnhat DESC", 0, 5, out outcount);
                datalistTieuDiem.DataSource = tintieudiem;
                datalistTieuDiem.DataBind();
                TList<TinTuc> tinsukien = DataRepository.TinTucProvider.GetPaged(sqlSuKien, "thoidiemcapnhat DESC", 0, 5, out outcount);
                datalistSuKien.DataSource = tinsukien;
                datalistSuKien.DataBind();
            }
            if (!string.IsNullOrEmpty(IdTinTuc) &&!string.IsNullOrEmpty(IdDanhmuc))
            {
                ListNewsOfCategory.Visible = false;
                NewsDetailOfCategory.Visible = true;
                int outcount;
                ID = IdTinTuc;
                sqlTieuDiem = "id_tintuc in (select id_tintuc from tb_tintuctieudiem) and id_tintucdanhmuc in (select id_tintucdanhmuc from tb_tintuc where id_tintuc='" + IdTinTuc + "') and id_tintuc <> '" + IdTinTuc + "' and nhacungcap is null";
                sqlSuKien = "id_tintuc in (select id_tintuc from tb_tintucsukien) and id_tintucdanhmuc in (select id_tintucdanhmuc from tb_tintuc where id_tintuc='" + IdTinTuc + "') and id_tintuc <> '" + IdTinTuc + "' and nhacungcap is null";
                sqlFindDanhmuc = "";
                sqlNewsOther = "id_tintucdanhmuc not in (select id_tintucdanhmuc from tb_tintuc where id_tintuc='" + IdTinTuc + "') and id_tintucdanhmuc in (select id_tintucdanhmuc from tb_tintucdanhmuc where id_tintucdanhmuccha in (select id_tintucdanhmuccha from tb_tintucdanhmuc where id_tintucdanhmuc in (select id_tintucdanhmuc from tb_tintuc where id_tintuc='" + IdTinTuc + "')))";
                TList<TinTuc> tintuc = DataRepository.TinTucProvider.Find("ID_TinTuc='"+IdTinTuc+"'");
                DataListGetNewsByID.DataSource = tintuc;
                DataListGetNewsByID.DataBind();
                TList<TinTuc> tintieudiem = DataRepository.TinTucProvider.GetPaged(sqlTieuDiem, "thoidiemcapnhat DESC", 0, 5, out outcount);
                datalistTieuDiem.DataSource = tintieudiem;
                datalistTieuDiem.DataBind();
                TList<TinTuc> tinsukien = DataRepository.TinTucProvider.GetPaged(sqlSuKien, "thoidiemcapnhat DESC", 0, 5, out outcount);
                datalistSuKien.DataSource = tinsukien;
                datalistSuKien.DataBind();
                TList<TinTucDanhMuc> tinOther = DataRepository.TinTucDanhMucProvider.GetPaged(sqlNewsOther, "", 0, 10, out outcount);
                listNews.DataSource = tinOther;
                listNews.DataBind();
            }
        }

        protected void ChuDe_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Label text = (Label)e.Item.FindControl("IDTinTucChuDe");
            DataList list = (DataList)e.Item.FindControl("datalistNewsChuDe");
            if (list != null && text != null && !string.IsNullOrEmpty(IdDanhmuc))
            {
                int outcount;
                string sql = "id_tintuc in (select id_tintuc from tb_tintuctheochude where id_tintucchude='" + text.Text + "') and nhacungcap is null and id_tintucdanhmuc in (select id_tintucdanhmuc from tb_TinTucDanhMuc where id_tintucdanhmuccha='" + IdDanhmuc + "')";
                TList<TinTuc> tintuc = DataRepository.TinTucProvider.GetPaged(sql, "thoidiemcapnhat DESC", 0, 5, out outcount);
                list.DataSource = tintuc;
                list.DataBind();
            }
        }
        protected void DataListGetNewsByID_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            DataList list = (DataList)e.Item.FindControl("datalistOtherNews");
            if (!string.IsNullOrEmpty(ID) && list != null&&!string.IsNullOrEmpty(IdTinTuc))
            {
                int outcount;
                string sqlOtherNews = "id_tintucdanhmuc in (select id_tintucdanhmuc from tb_tintuc where id_tintuc='" + ID + "') and id_tintuc <> '" + ID + "'";
                TList<TinTuc> tinKhac = DataRepository.TinTucProvider.GetPaged(sqlOtherNews, "thoidiemcapnhat DESC", 0, 10, out outcount);
                list.DataSource = tinKhac;
                list.DataBind();
            }
        }
        protected void listNews_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Label lb = (Label)e.Item.FindControl("IDTinTucDanhMuc");
            DataList list = (DataList)e.Item.FindControl("datalistNewsOther");
            string sql;
            int outcount;
            if (lb != null && list != null && !string.IsNullOrEmpty(IdTinTuc))
            {
                sql = "id_tintucdanhmuc = '" + lb.Text + "'";
                TList<TinTuc> tin = DataRepository.TinTucProvider.GetPaged(sql, "thoidiemcapnhat DESC", 0, 5, out outcount);
                list.DataSource = tin;
                list.DataBind();
            }
        }
    }
}
